####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Tab A11, A12
###
####################################################################################

library(AER)

################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
source(paste0(wd_code, '/01_data_load_clean.R'))


################################################################################
# Auxiliary Functions
################################################################################

cleantable = function(container){
  colnames(container)=c('coef', 'se', 'tval', 'pval', 'obs', 'mean', 'model')
  container[,1:6] = sapply(container[,1:6], as.numeric)
  container[,1:6] = round(container[,1:6], 3)
  container$se = paste0('(',  as.character(container$se), ')')
  container$coef = ifelse(container$pval<0.01, paste0(as.character(container$coef), '***'), 
                          ifelse(container$pval>0.01 & container$pval<0.05, paste0(as.character(container$coef), '**'),
                                 ifelse(container$pval>0.05 & container$pval<0.1, paste0(as.character(container$coef), '*'), as.character(container$coef))))
  
  container = t(container)
  print(container)
}


################################################################################
#  Table A11
###############################################################################


container = data.frame()
for (n in c(0, 1, 2, 3)){
  print(paste0('--------------Immigation tercile ', n, ' -------------- '))
  
  # Sample definition
  df1 = df_did[which(!is.na(df_did$fn)),]
  if (n>0){df1 = df1[which(df_did$imm_quant_99==n),]}
  
  print(paste0(' * With immigration shares as control *'))
  temp = lm(data=df1, fn ~  postT + imm + running + factor(CODGEO) + factor(policy_period) )
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  container = rbind(container, c(temp["postT",], attr(temp, 'nobs'), mean(df1$fn, na.rm = T), 'with imm controls'))
  
  print(paste0(' * With income and house controls *'))
  temp = lm(data=df1, fn ~  postT + log(median_inc) +  I(num_empty_log/num_log) + I(num_res_own/num_log) + running + factor(CODGEO) + factor(policy_period) )
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  container = rbind(container, c(temp["postT",], attr(temp, 'nobs'), mean(df1$fn, na.rm = T), 'with house controls'))
  
  print(paste0(' * With all controls *'))
  temp = lm(data=df1, fn ~  postT + log(median_inc) +  I(num_empty_log/num_log) + I(num_res_own/num_log) + imm + running + factor(CODGEO) + factor(policy_period) )
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  container = rbind(container, c(temp["postT",], attr(temp, 'nobs'), mean(df1$fn, na.rm = T), 'with all controls'))
  
}


cleantable(container)



################################################################################
#  Table A12
###############################################################################


container = data.frame()
for (n in c(1,2,3)){
  print(paste0('--------------Immigation tercile ', n, ' -------------- '))
  
  # Sample definition
  df1 = df_did[which(!is.na(df_did$fn) & df_did$imm_quant_99==n & !is.na(df_did$median_inc) & !is.na(df_did$num_empty_log)),]

  print(paste0(' * With NO controls *'))
  temp = lm(data=df1, fn ~  postT + running + factor(CODGEO) + factor(policy_period))
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  container = rbind(container, c(temp["postT",], attr(temp, 'nobs'), mean(df1$fn, na.rm = T), 'without controls'))

  print(paste0(' * With immigration shares as control *'))
  temp = lm(data=df1, fn ~  postT + imm + running + factor(CODGEO) + factor(policy_period) )
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  container = rbind(container, c(temp["postT",], attr(temp, 'nobs'), mean(df1$fn, na.rm = T), 'with imm controls'))

  print(paste0(' * With income and house controls *'))
  temp = lm(data=df1, fn ~  postT + log(median_inc) +  I(num_empty_log/num_log) + I(num_res_own/num_log) + running + factor(CODGEO) + factor(policy_period) )
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  container = rbind(container, c(temp["postT",], attr(temp, 'nobs'), mean(df1$fn, na.rm = T), 'with house controls'))

  print(paste0(' * With all controls *'))
  temp = lm(data=df1, fn ~  postT + log(median_inc) +  I(num_empty_log/num_log) + I(num_res_own/num_log) + imm + running + factor(CODGEO) + factor(policy_period) )
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  container = rbind(container, c(temp["postT",], attr(temp, 'nobs'), mean(df1$fn, na.rm = T), 'with all controls'))


}

cleantable(container)


